package com.example.mywanandroid.util;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.annotation.ColorRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;


import com.example.mywanandroid.config.App;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

/**
 * 工具类
 */
public final class CommonUtils {

    private CommonUtils() {

    }

    /**
     * 判断列表是否为空
     *
     * @param list 列表
     * @return true表示为空，false表示不为空
     */
    public static boolean isListEmpty(List<?> list) {
        return list == null || list.size() == 0;
    }

    public static boolean isStringEmpty(String string) {
        return string == null || "".equals(string);
    }

    /**
     * 获取字符串资源
     *
     * @param resId 字符串资源ID
     * @return 字符串
     */
    public static String getString(@StringRes int resId) {
        return App.getContext().getString(resId);
    }

    /**
     * 获取字符串资源
     *
     * @param i
     * @return 字符串
     */
    public static String int2String(@StringRes int i) {
        return i + "";
    }


    /**
     * 获取颜色资源
     *
     * @param resId 颜色资源ID
     * @return 颜色
     */
    public static int getColor(@ColorRes int resId) {
        return ContextCompat.getColor(App.getContext(), resId);
    }

    /**
     * 获取设备ID
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {

        String imei = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                imei = telephonyManager.getImei();
                if (imei == null || imei.trim().length() == 0) {
                    imei = telephonyManager.getMeid();
                }
            } else {
                imei = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
            if (imei == null || imei.trim().length() == 0) {
                imei = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (imei == null || imei.trim().length() == 0) ? "" : imei.trim();

    }

    /**
     * 获取本地IP地址
     *
     * @return
     */
    public static String getLocalIpAddress() {

        try {

            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {

        }

        return "";

    }

}
